package com.ocom.payconfig.controller;

import com.google.common.base.Joiner;
import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.payconfig.EntityDishesInfo;
import com.ocom.common.entity.payconfig.EntityPackageVoucher;
import com.ocom.common.entity.payconfig.EntityVoucherData;
import com.ocom.common.entity.payconfig.EntityVoucherType;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.NewVoucherTypeRequest;
import com.ocom.common.request.payconfig.UpdateVoucherTypeRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.DishesInfoService;
import com.ocom.payconfig.service.PackageVoucherService;
import com.ocom.payconfig.service.VoucherDataService;
import com.ocom.payconfig.service.VoucherTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class CouponController {

    @Autowired
    private VoucherTypeService voucherTypeService;

    @Autowired
    private VoucherDataService voucherDataService;

    @Autowired
    private PackageVoucherService packageVoucherService;

    @Autowired
    private DishesInfoService dishesInfoService;


    /**
     *@Description:消费券种类列表（voucherTypeList）
     * @author lu 
     * @param comId
     * @param attributeStr
     * @param consTypeStr 
     * @return com.ocom.common.resp.Result
     * @date 2023/2/2 9:44
     */
    @PostMapping(PayConfigUrl.Url_voucherTypeList)
    public Result voucherTypeList(Long comId,String attributeStr,String consTypeStr){
        List<EntityVoucherType> list=voucherTypeService.selectVoucherTypeList(comId,attributeStr,consTypeStr);
        List<EntityDishesInfo> dishesInfos = dishesInfoService.selectDishesInfoList(comId);
        Map<Integer, String> collect = dishesInfos.stream().collect(Collectors.toMap(e -> e.getDishesId(), e -> e.getDishesName(), (e1, e2) -> e1));
        List<Map<String,Object>> mapList=new ArrayList<>();
        for (EntityVoucherType entityVoucherType : list) {
            Map<String,Object> map=new HashMap<>();
            map.put("voucherTypeId",entityVoucherType.getVoucherTypeId());
            map.put("voucherTypeName",entityVoucherType.getVoucherTypeName());
            map.put("attribute",entityVoucherType.getAttribute());
            map.put("consType",entityVoucherType.getConsType());
            map.put("parValue",entityVoucherType.getParValue());
            map.put("maxNumber",entityVoucherType.getMaxNumber());
            map.put("dishes",entityVoucherType.getDishes());
            if (StringUtils.isNotBlank(entityVoucherType.getDishes())) {
                String[] split = entityVoucherType.getDishes().split(",");
                List<String> dishesList = new ArrayList<>();
                for (String dishesId : split) {
                    String s = collect.get(Integer.valueOf(dishesId));
                    if (StringUtils.isNotBlank(s)) {
                        dishesList.add(s);
                    }
                }
                map.put("dishesName", Joiner.on(",").join(dishesList));
            }
            map.put("remark",entityVoucherType.getRemark());
            mapList.add(map);
        }
        return ResultUtil.success(mapList);
    }

    /**
     *@Description:删除消费券种类（removeVoucherType）
     * @author lu 
     * @param comId
     * @param voucherTypeId
     * @return com.ocom.common.resp.Result
     * @date 2023/2/3 10:45       
     */
    @PostMapping(PayConfigUrl.Url_removeVoucherType)
    public Result removeVoucherType(Long comId,Long voucherTypeId){
        EntityPackageVoucher entityPackageVoucher=packageVoucherService.selectVoucherTypeId(comId,voucherTypeId);
        if (entityPackageVoucher!=null){
            return ResultUtil.error(ResultEnum.Err_4072);
        }
        EntityVoucherData entityVoucherData=voucherDataService.selectVoucherType(comId,voucherTypeId);
        if (entityVoucherData!=null){
            return ResultUtil.error(ResultEnum.Err_4073);
        }
        boolean b=voucherTypeService.removeVoucherType(comId,voucherTypeId);
        if (b){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.Err_3016);
        }
    }

    /**
     *@Description:添加消费券种类（newVoucherType）
     * @author lu
     * @param newVoucherTypeRequest
     * @return com.ocom.common.resp.Result
     * @date 2023/2/2 11:26
     */
    @PostMapping(PayConfigUrl.Url_newVoucherType)
    public Result newVoucherType(@Valid @RequestBody NewVoucherTypeRequest newVoucherTypeRequest){
        EntityVoucherType entityVoucherType=voucherTypeService.selectVoucherType(newVoucherTypeRequest.getComId(),newVoucherTypeRequest);
        if (entityVoucherType != null){
            return ResultUtil.error(ResultEnum.Err_4071);
        }
        if (voucherTypeService.newVoucherType(newVoucherTypeRequest.getComId(),newVoucherTypeRequest)){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.Err_2016);
        }
    }

    /**
     *@Description:修改消费券种类
     * @author lu 
     * @param updateVoucherTypeRequest 
     * @return com.ocom.common.resp.Result
     * @date 2023/2/2 14:57       
     */
    @PostMapping(PayConfigUrl.Url_updateVoucherType)
    public Result updateVoucherType(@Valid @RequestBody UpdateVoucherTypeRequest updateVoucherTypeRequest){
        EntityVoucherType entityVoucherType=voucherTypeService.selectUVoucherType(updateVoucherTypeRequest.getComId(),updateVoucherTypeRequest);
        if (entityVoucherType != null){
            return ResultUtil.error(ResultEnum.Err_4071);
        }
        if (voucherTypeService.updateVoucherType(updateVoucherTypeRequest.getComId(),updateVoucherTypeRequest)){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.Err_3014);
        }
    }
}
